package com.jn.gsondemo.views;

import android.content.Intent;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.jn.gsondemo.R;
import com.jn.gsondemo.base.BaseActivity;
import com.jn.gsondemo.entity.Data;
import com.jn.gsondemo.entity.People;
import com.jn.gsondemo.entity.School;
import com.jn.gsondemo.entity.Student;
import com.jn.gsondemo.utils.JsonUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private Student mStudent;
    private Student mStudent1;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void init() {
        //objToJson();
        //jsonToObj();
        //getJson("news.json");
        //objToJson2();
        //jsonToObj2();
        //jsonToObj3();
        //jsonToList();
        //testJsonArray();
        //getValueByKey();
        testJsonParser();

        findViewById(R.id.book).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, BookActivity.class);
                startActivity(intent);
            }
        });

        findViewById(R.id.hotsearch).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, HotsearchActivity.class);
                startActivity(intent);
            }
        });

        findViewById(R.id.olympic).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, OlympicActivity.class);
                startActivity(intent);
            }
        });
    }

    private String objToJson() {
        mStudent = new Student();
        mStudent.setStudent_code("22");
        mStudent.setStudent_id(1);
        mStudent.setStudent_name("刘老八");
        mStudent.setUsername("abcdef");
        mStudent.setPassword("123456");
        mStudent.setClass_name("计算机10班");
        Gson gson = new Gson();
        String jsonStr = gson.toJson(mStudent);
        Log.d("asdf", "objToJson : " + jsonStr);
        return jsonStr;
    }

    private void jsonToObj() {
        String jsonStr = objToJson();
        Gson gson = new Gson();
        mStudent1 = gson.fromJson(jsonStr, Student.class);
        Log.d("asdf", "jsonToObj = " + mStudent1.toString());
    }

    private void objToJson2() {
        Student student = new Student();
        student.setStudent_code("22");
        student.setStudent_id(1);
        student.setStudent_name("刘老八");
        student.setUsername("abcdef");
        student.setPassword("123456");
        student.setClass_name("计算机10班");

        Student student2 = new Student();
        student2.setStudent_code("22");
        student2.setStudent_id(1);
        student2.setStudent_name("刘老八");
        student2.setUsername("abcdef");
        student2.setPassword("123456");
        student2.setClass_name("计算机10班");
        List<Student> students = new ArrayList<>();
        students.add(student);
        students.add(student2);

        School school = new School();
        school.setName("吉林农业科技学院");
        school.setAddress("吉林市");
        school.setStudents(students);

        Gson gson = new Gson();
        String jsonStr = gson.toJson(school);
        Log.d("asdf", "school = " + jsonStr);
    }

    private void jsonToObj2() {
        String jsonStr = getJson("school.json");
        Gson gson = new Gson();
        School school = gson.fromJson(jsonStr, School.class);
        Log.d("asdf", "jsonToObj = " + school.toString());
    }

    private void jsonToObj3() {
        String jsonStr = getJson("student_list.json");
        Gson gson = new Gson();
        Data student_list = gson.fromJson(jsonStr, Data.class);
        Log.d("asdf", "jsonToObj = " + student_list.getData().getGrade2021());
    }

    private void jsonToList() {
        Gson gson = new Gson();
        String jsonStr = getJson("peoples.json");
        //Type type = new TypeToken<List<People>>() {}.getType();
        //List<People> list = gson.fromJson(jsonStr, type);

        List<People> list = JsonUtils.jsonToList(jsonStr, People.class);

        for (People people : list) {
            Log.d("asdf", "people = " + people.toString());
        }
    }

    private void testJsonArray() {
        JsonArray jsonArray = new JsonArray();
        jsonArray.add("老张");
        jsonArray.add("老李");
        jsonArray.add("老王");
        String jsonStr = jsonArray.toString();
        Log.d("asdf", "json array = " + jsonStr);
    }

    private void getValueByKey() {
        String jsonStr = getJson("news.json");
        String value = JsonUtils.getKeyRecursively(jsonStr, "thumbnail_pic_s");
        Log.d("asdf", "getValueByKey value ==== " + value);
    }

    private void testJsonParser() {
        String jsonStr = getJson("student_list.json");
        JsonObject jo = JsonParser.parseString(jsonStr).getAsJsonObject();
        JsonObject data = jo.get("data").getAsJsonObject();
        JsonArray array = data.get("grade2021").getAsJsonArray();
        for (JsonElement jsonElement : array) {
            String name = jsonElement.getAsJsonObject().get("student_name").getAsString();
            Log.d("asdf", " ==name== " + name);
        }
    }

    private String getJson(String name) {
        String json = JsonUtils.getJsonFromAssets(this, name);
        Log.d("asdf", "getJson = " + json);
        return json;
    }
}